﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SimpleRssReader.Model;
using SimpleRssReader.ViewModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows8Accelerator.Core.Xaml;
using Windows8Accelerator.Core.Xaml.Helpers;
using Windows.System;
using Windows8Accelerator.Core.Xaml.Converters;
using Windows8Accelerator.Core.Xaml.Controls;
using Windows.UI;
using Windows.UI.ViewManagement;
using SimpleRssReader.Pages;
using Windows8Accelerator.Core.Xaml.Html;

namespace SimpleRssReader
{
    public sealed partial class PostPage : Page
    {
        public PostViewModel ViewModel
        {
            get { return DataContext as PostViewModel; }
        }

        public PostPage()
        {
            this.InitializeComponent();
            SuperListView.GroupHeaderClick += PostViewModel.GroupHeaderClick;
            SuperListView.ItemClick += PostViewModel.ItemClick;
            SuperRichTextBlock.ImageClick += SuperRichTextBlock_ImageClick;
            SuperRichTextBlock.HyperlinkClick += SuperRichTextBlock_HyperlinkClick;
        }

        void SuperRichTextBlock_ImageClick(object sender, ImageClickEventArgs e)
        {
            ShowImageFlyout(e.Src);
            e.Handled = true;
        }

        void SuperRichTextBlock_HyperlinkClick(object sender, HyperlinkClickEventArgs e)
        {
            if (e.Href.ToLower().EndsWith(".jpg") ||
                e.Href.ToLower().EndsWith(".jpeg") ||
                e.Href.ToLower().EndsWith(".gif") ||
                e.Href.ToLower().EndsWith(".bmp") ||
                e.Href.ToLower().EndsWith(".png"))
            {
                ShowImageFlyout(e.Href);
                e.Handled = true;
            }
            else
            {
                //ShowWebViewFlyout(e.Href);
                NavigateToWebViewPage(e.Href);
                e.Handled = true;
            }
        }

        private void NavigateToWebViewPage(string href)
        {
            Frame.Navigate(typeof(WebViewPage), href);
        }

        private void ShowWebViewFlyout(string href)
        {
            if (ApplicationView.Value != ApplicationViewState.Snapped)
            {
                var widthMargin = Window.Current.Bounds.Width * 0.1;
                var heightMargin = Window.Current.Bounds.Height * 0.1;
                SuperFlyoutHelper.ShowSuperWebViewFlyout(
                    this.Content as Panel,
                    href,
                    new SolidColorBrush(Colors.White),
                    new Thickness(0),
                    new Thickness(widthMargin, heightMargin, widthMargin, heightMargin),
                    new SolidColorBrush(Colors.Black),
                    new Thickness(4)
                    );
            }
        }

        private void ShowImageFlyout(string imageSrc)
        {
            if (ApplicationView.Value != ApplicationViewState.Snapped)
            {
                SuperFlyoutHelper.ShowSuperImageFlyout(
                    this.Content as Panel,
                    imageSrc,
                    new SolidColorBrush(Colors.White),
                    new Thickness(20),
                    new SolidColorBrush(Colors.Black),
                    new Thickness(4)
                    );
            }
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            if (ViewModel == null)
            {
                DataContext = AppHelperBase.Instance.GetOrCreatePageViewModel<PostViewModel>(this);
            }
            var post = (Post)e.Parameter;
            await ViewModel.LoadAsync(post);            
        }

        private async void BrowserButtonClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;
            await Launcher.LaunchUriAsync(new Uri(button.Tag as string));
        }

    }
}
